<?php

class ModUser extends model
{
	// Active user row.. default = null
	private $activeUser = null;
	
	// Set functions
	public function setNickname($n) 		{ return $this->setUserField('nickname', $n); 			} // end function
	public function setUsername($n) 		{ return $this->setUserField('login', $n); 				} // end function
	public function setEmail($n) 			{ return $this->setUserField('email', $n); 				} // end function
	public function setStatus($n) 			{ return $this->setUserField('status', $n); 			} // end function
	public function setStatusDetailed($n) 	{ return $this->setUserField('status_de', $n); 			} // end function
	public function setLastTime() 			{ return $this->setUserField('last_active', time()); 	} // end function
	
	// Get functions
	public function getNickname() 			{ return $this->getUserField('nickname'); 		} // end function
	public function getUsername() 			{ return $this->getUserField('login'); 			} // end function
	public function getEmail() 				{ return $this->getUserField('email'); 			} // end function
	public function getStatus() 			{ return $this->getUserField('status');			} // end function
	public function getStatusDetailed() 	{ return $this->getUserField('status_de'); 		} // end function
	public function getID() 				{ return $this->getUserField('id'); 			} // end function
	public function getLastTime() 			{ return $this->getUserField('last_active'); 	} // end function
	
	// Underlying set function for user fields
	private function setUserField($f, $v)
	{
		if ($this->activeUser == null)
			return false;
		
		if (!isset($this->activeUser[$f]))
			return false;
		else
		{
			$this->query("UPDATE users SET `" . $f . "` = '" . addslashes($v) . "' WHERE id = '" . $this->activeUser['id'] . "'");
			
			$this->activeUser[$f] = $v;
			
			return $this->activeUser[$f];
			
		} // end function
		
	} // end function
	
	// Underlying get function for user fields
	private function getUserField($f)
	{
		if ($this->activeUser == null)
			return false;
		
		if (!isset($this->activeUser[$f]))
			return false;
		else
			return $this->activeUser[$f];
			
	} // end function
	
	// Set the current active user
	public function setUser($u, $forceUname = false)
	{
		if (($u == -1 || is_numeric($u)) && $forceUname == false)
			$where = "id = '" . $this->getSession('user') . "';";
		else
			$where = "login = '" . $u . "';";
		
		$sUser = $this->query("SELECT * FROM users WHERE " . $where);
		
		if ($this->num($sUser) == 0)
			return false;
		
		$this->activeUser = $this->fetch($sUser);
		
		return true;
		
	} // end function
	
	// Make a hash for the password (login section)
	private function getHashFromPassword($p)
	{
		$p = md5($p . $this->getSession('login_seed'));
		
		return $p;
		
	} // end function

	// Check if the login information is correct.
	public function check($u, $p)
	{
		$sUser = $this->query("SELECT * FROM users WHERE login = '" . $u . "'");
		
		if ($this->num($sUser) == 1)
		{
			$fUser = $this->fetch($sUser);
			
			if ($this->getHashFromPassword($fUser['password']) == $p)
				return true;
		
		} // end if
		
		return false;
	} // end function
	
	// Get a certain user
	public function getUser($i, $field = 'id')
	{
		$w = (($field == 'username') ? "`login` = '" . $i . "'" : "`id` = '" . $i . "'");
		
		$sUser = $this->query("SELECT * FROM users WHERE " . $w);
		
		if ($this->num($sUser) == 0)
			return;
		else
		{
			return $this->fetch($sUser);
			
		} // end else
		
	} // end function
	
	// Set the status to active
	public function setActive()
	{
		// Get the user status
		$s = $this->getStatus();
		
		if (!$s || $s > 0)
			return false;
		
		// Update the user status to a negative number so we know it's logged out
		$this->setStatus(0 - $s);
		
	} // end function
	
	// Kick the user (logging out)
	public function Logout()
	{
		// Get the user status
		$s = $this->getStatus();
		
		if (!$s)
			return false;
		
		// Update the user status to a negative number so we know it's logged out
		$this->setStatus(0 - $s);
		
	} // end function
	
	// Log out all inactive users (that didn't gave any response within 15 seconds from now)
	public function log_out_inactive($m)
	{
		$s = $this->query("SELECT * FROM users WHERE status > 0 AND last_active < '" . (time() - 15) . "'");
		
		while ($f = $this->fetch($s))
		{
			// Save the action
			$this->act($m, $f['id'] . ',status,' . (0-$f['status']) , 'contacts');
			
			$this->query("UPDATE users SET status = 0 - status WHERE id = '" . $f['id'] . "'");
			
		} // end while
		
	} // end function
	
} // end class

$_ = 'ModUser';

?>